//app.js
require('./config/config.js');                
require('./common.js');
require('./common/common.js');
require('./api/index.js');                  // 全局接口调用    wx.$api.
require('./common/icon.js');                // 全局图标引入
require('./common/component.js')(wx);
App({
  onLaunch: function () {
    wx.$facility();
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())

    // 登录
    wx.login({
      success: res => {
        let that = this;
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          return  new Promise((resolve, reject) => {
            wx.request({
              url: wx.$config.serverAddress + 'api/weixinLogin',
              data: { data: { code: res.code, from: 'member' } },
              header: {
                'content-type': 'application/json'
              },
              success(response) {
                console.log('response', response);
                if (response.data.code == 200) {
                  let data = response.data.data || {}, sets = wx.setStorageSync;
                  let { token, user_id, is_certification, member_mobile } = data || {};
                  wx.setStorageSync('token', token);
                  wx.setStorageSync('user_id', user_id);
                  wx.setStorageSync('member_mobile', member_mobile);
                  wx.setStorageSync('is_certification', is_certification);
                  sets("login", true)
                  wx.$facility();
                  console.log(getApp())
                }
              }
            })
          })
        }
      }
    })
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              this.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.userInfoReadyCallback) {
                this.userInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  globalData: { 
    userInfo: null
  },
  config:{
    openid:'',
    session_key:'',
  }

})